<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>浅深拷贝</title>
</head>
<body>
  
</body>
<script>
  // 模块化开发
  window.onload = function(){
    init();
  }
  function init(){
    console.log(initFb(10));
    console.log(initDiv(100));
  }

  var initFb = (function(){
    function fn(n){
      if(n <= 0){
        return 0;
      }
      if(n < 2){
        return 1;
      }
      return fn(n - 1) + fn(n - 2);
    }
    return fn;
  })();

  var initDiv = (function(){
    var arr = []
    function Div(n){
      for(let i = 1; i <= n; i++){
        if(i % 3 === 0 || i % 5 === 0 || i % 7=== 0){
          arr.push(i);
        }
      }
      return arr
    }
    return Div;
  })()

  // 浅拷贝//复制//克隆
  Object.prototype.num = 1;
  var person1 = {
    name: "张三",
    age: 18,
    sex: "male",
    heihgt: 175,
    weight: 140,
    sem: {
      liet: "abc"
    }
  }

  var person2 = {}
  // 浅拷贝
  function clone(origin, target){
    for(let key in origin){
      if(origin.hasOwnProperty(key)){
        target[key] = origin[key];
      }
    }
  }

  //深拷贝
  function deepClone(origin, target){
    var target = target || {};
    var toStr = Object.prototype.toString;
    var arrtype = '[object Array]';

    for(let key in origin){
      if(origin.hasOwnProperty(key)){
        if(typeof(origin[key]) === 'object' && origin[key] !== null){
          if(toStr.call(origin[key] === arrtype)){
            target[key] = {};
          }else{
            target[key] = [];
          }
          // target[key] = toStr.call(origin(key)) === arrtype ? [] : {};
          deepClone(origin[key], target[key]);
        }else{
          target[key] = origin[key];
        }
      }
    }
    return target;
  }
  //深拷贝
  function deepClone_2(startObj, endObj){
    var obj = endObj || {};
    for(var i in startObj){
      if(typeof startObj[i] === "object"){
        obj[i] = startObj[i].constructor === Array ? [] : {};
        deepClone_2(startObj[i], obj[i]);
      }else{
        obj[i] = startObj[i]
      }
    }
    return obj;
  }
  deepClone(person1, person2);
  person2.name = "李四";
  person2.sem.liet = "18";
  console.log(person1, person2);

  var objitem = {
    name: "xxx名",
    age: "18",
    color: "red",
    obj: {
      item: "电影"
    }
  }
  var objCopy = deepClone_2(objitem);
  objitem.obj.item = "20";
  console.log(objitem);
  console.log(objCopy);

  function Foo(){
    getName = function(){
      console.log(1);
    }
    return this;
  }
  Foo.getName = function(){
    console.log(2);
  }
  Foo.prototype.getName = function(){
    console.log(3);
  }
  var getName = function(){
    console.log(4);
  }
  function getName(){
    console.log(5);
  }
  Foo.getName();          //2
  getName();              //4
  Foo().getName();        //1
  getName();              //1`                  
  new Foo.getName();      //2
  new Foo().getName();    //3
  new new Foo().getName();//3

  
</script>
</html>